package com.outlook.flowervowlogin;

import org.apache.commons.lang.RandomStringUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;
import java.util.*;

public class EventJoin implements Listener {
    static Map<Player,BukkitRunnable> TASK = new HashMap<>();
    static Map<Player,BukkitRunnable> TASK2 =new HashMap<>();
    private Main plugin;
    public EventJoin(Main plugin){
        this.plugin = plugin;
    }
    @EventHandler
    public void onJoin(PlayerJoinEvent e){
        Player p = e.getPlayer();
        String word = RandomStringUtils.randomAlphanumeric(4);
        CommandCaptcha.CAPTCHA.put(p,word);
        e.setJoinMessage(null);
        try {
            if (CommandCaptcha.CAPTCHA.containsKey(p)) {
              TASK.put(p,new BukkitRunnable(){
                    @Override
                    public void run(){
                        p.sendMessage("§7[§a|§7]§a请输入 §b/captcha "+    CommandCaptcha.CAPTCHA.get(p)+"§a来验证您的账号");
                    }
                });
              TASK.get(p).runTaskTimer(plugin,0,50);
                TASK2.put(p,new BukkitRunnable(){
                    @Override
                    public void run(){
                        p.kickPlayer("§a[§a茶§b坂]§c超时登录");
                    }
                });
                TASK2.get(p).runTaskLater(plugin,450);
            }else {
                File dir = new File(Main.instance.getDataFolder(), "Player");
                File playerfile = new File(dir, p.getName() + ".yml");
                if (!playerfile.exists()) {
                    p.sendMessage("§a[§a茶§b坂]§6请输入 §a/reg [密码] [重复密码] §6来注册你的账号");
                } else {
                    p.sendMessage("§a[§a茶§b坂]§6请输入§a/l [密码] §6来登录你的账号");
                }
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.kickPlayer("§a[§a茶§b坂]§c超时登录");
                    }
                }.runTaskLater(plugin, 500);
            }
        }catch (Exception error){
            System.out.println("");
        }

    }
}
